package com.hellogood.yaohuo.server.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.db.DBConnection;
import com.hellogood.yaohuo.server.db.DBHelper;
import com.hellogood.yaohuo.shared.entity.Ziyou;
import com.hellogood.yaohuo.shared.exception.DatabaseException;

public class ZiyouDAO extends DBHelper {
	Logger log = Logger.getLogger(this.getClass());

	@Override
	public List<?> initializeObject(ResultSet rs) throws SQLException {

		List<Ziyou> ziyouList = new ArrayList<Ziyou>();
		if (rs != null) {
			while (rs.next()) {
				Ziyou z = new Ziyou();
				z.setZiyouCode(rs.getString("ZiyouCode"));
				z.setZiyouName(rs.getString("ZiyouName"));
				ziyouList.add(z);
			}
		}
		return ziyouList;

	}

	@SuppressWarnings("unchecked")
	public List<Ziyou> getAllZiyou() {
		String sql = "SELECT * FROM Ziyou ORDER BY ziyouCODE";
		List<?> ziyouList = super.query(sql);
		return (List<Ziyou>) ziyouList;
	}

	public Ziyou getZiyouByCode(Ziyou z) {
		String sql = "SELECT * FROM Ziyou WHERE ziyouCode = '" + z.getZiyouCode() + "'";
		List<?> ziyouList = super.query(sql);
		if (ziyouList != null && ziyouList.size() > 0) {
			return (Ziyou) ziyouList.get(0);
		}
		return null;
	}

	public Ziyou getZiyouByName(Ziyou z) {
		String sql = "SELECT * FROM Ziyou WHERE ziyouName = '" + z.getZiyouName() + "'";
		List<?> ziyouList = super.query(sql);
		if (ziyouList != null && ziyouList.size() > 0) {
			return (Ziyou) ziyouList.get(0);
		}
		return null;
	}

	public String addNewZiyou(Ziyou z) {
		String sql = "INSERT INTO Ziyou(ZiyouCODE,ZiyouNAME) VALUES('" + z.getZiyouCode() + "','" + z.getZiyouName() + "')";
		try {
			if (super.execute(sql) > 0) {
				return "";
			}
		} catch (DatabaseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}

	public String ModifyZiyou(Ziyou z) {
		String sql = "UPDATE Ziyou SET ZiyouNAME = '" + z.getZiyouName() + "' WHERE ZiyouCode = '" + z.getZiyouCode() + "'";
		try {
			if (super.execute(sql) > 0) {
				return "";
			}
		} catch (DatabaseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}

	public String getLastCode() {
		String lastCode = null;
		// Connection conn = ConnectionPool.getInstance().getConnection();
		Connection conn = DBConnection.getInstance().getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "SELECT TOP 1 ZiyouCode FROM Ziyou ORDER BY ZiyouCode DESC";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				lastCode = rs.getString("ZiyouCode");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			log.error(sql);
			log.error(ex.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				// ConnectionPool.getInstance().release(conn);
			}
		}
		return lastCode;
	}

}
